package ui.forms;

import base.User;
import base.Calculator;
import base.Sex;
import utils.DateUtils;
import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.*;
import ui.NumericField;

/**
 * Defines the layout for the form.Displays the BMI.
 * User can change the height and weight input.
 *
 * @author Meera, Shayan
 */
public class BMIForm extends Form implements ActionListener {

    private TextArea lbMsg;
    private ButtonGroup grpSex;
    private RadioButton rduMale;
    private RadioButton rduFemale;
    private ComboBox lstHeightUnit;
    private ComboBox lstWeightUnit;
    private NumericField txtHeight;
    private NumericField txtWeight;
    private Command cmdBack;
    private Button btnCalc;
    private float BMI;
    private String wtState;
    private float[] wtRange;

    public BMIForm(final Form parent) {

        cmdBack = new Command("Back") {

            public void actionPerformed(ActionEvent evt) {
                parent.showBack();
            }
        };
        addCommand(cmdBack);
        setBackCommand(cmdBack);

        lbMsg = new TextArea("", 3, 7);
        lbMsg.setPreferredH((getPreferredH()) / 4);
        lbMsg.setConstraint(TextArea.UNEDITABLE);

        btnCalc = new Button("Calculate");
        btnCalc.addActionListener(this);
        wtState = new String();
        wtRange = new float[2];
        lstWeightUnit = new ComboBox();
        lstWeightUnit.getStyle().setBgTransparency(0);

        lstHeightUnit = new ComboBox();
        lstHeightUnit.getStyle().setBgTransparency(0);

        lstWeightUnit.addItem("kg");
        lstWeightUnit.addItem("pound");
        lstHeightUnit.addItem("cm");
        lstHeightUnit.addItem("ft");
        lstWeightUnit.setPreferredW(lstHeightUnit.getPreferredW());

        grpSex = new ButtonGroup();
        rduMale = new RadioButton("Male");
        rduMale.getStyle().setBgTransparency(0);
        rduFemale = new RadioButton("Female");
        rduFemale.getStyle().setBgTransparency(0);
        grpSex.add(rduMale);
        grpSex.add(rduFemale);


        Container mainContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        mainContainer.addComponent(new Label("Username: " +
                Context.currentUser.getName()));

        mainContainer.addComponent(new Label("Date of Birth: " +
                DateUtils.DatetoString(Context.currentUser.getDateOfBirth())));

        Container cntSex = new Container(new GridLayout(1, 2));
        cntSex.addComponent(rduMale);
        cntSex.addComponent(rduFemale);
        mainContainer.addComponent(cntSex);

        Container cnt2 = new Container(new BoxLayout(BoxLayout.X_AXIS));
        txtHeight = new NumericField("Your Height");
        cnt2.addComponent(txtHeight);
        cnt2.addComponent(lstHeightUnit);

        mainContainer.addComponent(cnt2);
        Container cntWeight = new Container(new BoxLayout(BoxLayout.X_AXIS));

        txtWeight = new NumericField("Your Weight");
        cntWeight.addComponent(txtWeight);

        cntWeight.addComponent(lstWeightUnit);
        mainContainer.addComponent(cntWeight);

        Container cntButtons = new Container(new FlowLayout(Component.CENTER));
        cntButtons.addComponent(btnCalc);
        mainContainer.addComponent(cntButtons);
        mainContainer.addComponent(lbMsg);

        addComponent(mainContainer);
        addCommandListener(this);

        this.setUserData();
    }

    public void setUserData() {
        User user = Context.currentUser;

        if (user.getHeight() != 0) {
            txtHeight.setText(Float.toString(user.getHeight()));
        }
        if (user.getWeight() != 0) {
            txtWeight.setText(Float.toString(user.getWeight()));
        }
        if (user.getSex() == Sex.Male) {
            grpSex.setSelected(rduMale);
        } else {
            grpSex.setSelected(rduFemale);
        }
        BMI = Calculator.getBMI(user.getHeight(), user.getWeight(), "cm", "kg");
        wtState = getWeightState(BMI);
        wtRange = Calculator.getWtRange(user.getHeight(), "cm");
        lbMsg.setText("Your BMI is " + BMI + '\n' + "You are " + wtState + '\n' + "You should be within " + (int) wtRange[0] + " to " + (int) wtRange[1] + " kg.");

    }

    public String getWeightState(float bmi) {

        if (bmi < 18.5) {
            return "UNDERWEIGHT";
        } else if (bmi > 24.9) {
            return "OVERWEIGHT";
        } else {
            return "NORMAL";
        }

    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == btnCalc) {
            BMI = Calculator.getBMI(
                    Float.parseFloat(txtHeight.getText()),
                    Float.parseFloat(txtWeight.getText()),
                    lstHeightUnit.getSelectedItem().toString(),
                    lstWeightUnit.getSelectedItem().toString());

            wtState = getWeightState(BMI);
            wtRange = Calculator.getWtRange(
                    Float.parseFloat(txtHeight.getText()),
                    lstHeightUnit.getSelectedItem().toString());
            lbMsg.setText("Your BMI is " + BMI + '\n' + "You are " + wtState + '\n' + "You should be within " + (int) wtRange[0] + " to " + (int) wtRange[1] + " kg.");
        }

    }
}
